// pages/goodsClassifyDetail/goodsClassifyDetail.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: getApp().globalData.IMG_URL,
    type: -1,
    classifySubList: Array,
    tradeGoodsPage: Array,
    itemViewBg: "#FFFFFF",
    itemViewActiveBg: "#F6F6F6",
    selectedIndex: 0,
    hiddenRightView: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    var pageObj = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function (param) {
      console.error("参数：" + JSON.stringify(param))
      pageObj.setData({ type: param.type })
      wx.setNavigationBarTitle({ title: param.name })

      pageObj.onPullDownRefresh()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.initLeftViewData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  gotoGoodsDetail(param: any): void { // 打开商品详情
    wx.navigateTo({
      url: '/pages/goods/goodsDetail/goodsDetail',
      events: {
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { goodsId: param.currentTarget.dataset.id })
      }
    })
  },
  initLeftViewData() {
    var that = this
    const token = wx.getStorageSync('token')
    const preUrl = getApp().globalData.SERVER_URL
    const goodsPageForHomeUrl = preUrl + "fc/emall/industry/listForLeftOrRight"
    console.error("goodsPageForHomeUrl=" + goodsPageForHomeUrl)
    console.error("initLeftViewData()：pid=" + this.data.type)
    wx.request({
      url: goodsPageForHomeUrl,
      method: 'GET',
      data: {
        pid: this.data.type,
        pageNo: 1,
        pageSize: 100
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        'X-Access-Token': token
      },
      success(res: any) {
        wx.stopPullDownRefresh()
        if (res.data.code == 200) {
          console.error("成功：" + JSON.stringify(res))

          that.setData({ classifySubList: res.data.result.records })
          console.error(JSON.stringify(that.data.classifySubList))

          that.setData({ selectedIndex: that.data.classifySubList[0].id })
          that.onLeftItemClick(that.data.classifySubList[0].id);

          that.setData({ hiddenRightView: false })
        } else {
          console.error("失败："+JSON.stringify(res))
        }
      },
    })
  },
  onLeftViewItemClick(event) {
    const dataset = event.currentTarget.dataset
    const tradeId = dataset.id

    this.setData({ selectedIndex: tradeId })

    console.error("tradeIdInput ==> " + tradeId)
    this.onLeftItemClick(tradeId)
  },
  onLeftItemClick(tradeId: string) {
    console.error("tradeId ==> " + tradeId)
    var that = this
    const token = wx.getStorageSync('token')
    const preUrl = getApp().globalData.SERVER_URL
    const goodsPageForHomeUrl = preUrl + "fc/emall/goods/loadSubTradeGoodsPage"
    wx.request({
      url: goodsPageForHomeUrl,
      method: 'POST',
      data: {
        tradeId: tradeId,
        pageNum: 1,
        pageSize: 100
      },
      header: {
        'content-type': 'application/json', // 默认值
        'X-Access-Token': token
      },
      success(res: any) {
        console.error("onLeftItemClick.res ==> " + JSON.stringify(res))
        
        wx.stopPullDownRefresh()
        if (res.data.code == 200) {
          const length = res.data.result.records.length
          for (let index = 0; index < length; index++) {
            const item = res.data.result.records[index]
            if (item != null) {
              const cover = item.goodsCover;
              if (cover.includes(',')) {
                 // console.error("cover ==> " + cover)
                const firstIndex = cover.indexOf(',');
                const coverReal = cover.substring(0, firstIndex);
                // console.error("coverReal ==> " + coverReal)
                item.goodsCover = coverReal
              } else {
                item.goodsCover = cover
              }
             
              if (item.goodsName.length > 16) {
                item.goodsName = item.goodsName.substring(0, 16) + "...";
              }
            }
          }
          that.setData({ tradeGoodsPage: res.data.result.records })
          console.info("tradeGoodsPage" + JSON.stringify(that.data.tradeGoodsPage))
        } else {
          console.info(JSON.stringify(res))
        }
      },
    })
  }
})